<?php
/**
 * Created by PhpStorm.
 * User: xuke
 * Date: 16/6/30
 * Time: 下午5:39
 */

namespace App\Logic;
use App\Http\Model\UserCart AS UserCartModel;
use ApiException;
use Illuminate\Database\Eloquent\Collection;

class UserCartsLogic extends BaseLogic
{
    protected $_userCart = null;
    const SKU_TYPE = 'sku';
    const BUNDLE_TYPE = 'bundle';

    public function __construct($userCart)
    {
        $this->_userCart = $userCart;
    }

    public static function initByUserCart($userCart)
    {
        return new static($userCart);
    }

    public function getUserCart()
    {
        return $this->_userCart;
    }

    public static function getUserCartList($user_id, $pagesize = 15)
    {
        $builder = static::getBuilder(['userId' => $user_id]);
        $list = $builder->paginate($pagesize);

        return new static($list);
    }


    /**  查看购物车是否有某个商品,有就返回购物车模型,否则返回false
     * @param $sku_id  sku_id或者bundle_id
     * @param $type    商品类型,sku或者bundle
     * @return bool|userCart
     */
    public function checkIshasUserCart($sku_id, $type)
    {
        $userCartList = $this->getUserCart()->keyBy(function($item){
            return $item['sku_id']."_".$item['bundle_id'];
        });
        if ($type == self::SKU_TYPE)
        {
            $key = $sku_id.'_0';
        }else if($type == self::BUNDLE_TYPE)
        {
            $key = '0_'.$sku_id;
        }
        if ($userCartList->has($key))
        {
            return $userCartList->get($key);
        }else{
            return false;
        }
    }

    public function loadGoods()
    {
        $this->_userCart->load('sku.goods');
        return $this;
    }


    public function loadBundle()
    {
        $this->_userCart->load('bundle.bundleGoods');
        return $this;
    }


    
    public function toApi($userCarts)
    {
        $userCarts = $this->loadBundle()->loadGoods()->getUserCart();
        $re  = [];
        foreach ($userCarts as $usercart)
        {
            $data = [];
            $data['id'] = (int)$usercart->cart_id;
            $data['cart_type'] = (string)$usercart->cart_type;
            $data['sku_id'] = (int)$usercart->sku_id;
            $data['bundle_id'] = (int)$usercart->bundle_id;
            $data['price'] = (float)$usercart->price;
            $data['count'] = (int)$usercart->count;
            if ($usercart->cart_type == UserCartLogic::SKU_TYPE)
            {
                $data['goods_name'] = (string)$usercart->sku->goods->name;
                $data['sku_name'] = (string)$usercart->sku->sku_name;
                $data['price'] = (float)$usercart->sku->price;
            }else if($usercart->cart_type == UserCartLogic::BUNDLE_TYPE){
                $data['goods'] = self::apiBundleGoods($usercart->bundle->bundleGoods);
            }
            $re[] = $data;
        }
        return $re;
    }

    public static function apiBundleGoods($bundleGoods)
    {
        $data = [];
        foreach ($bundleGoods as $bgoods)
        {
            $bgoods->load('sku','goods');
            $re['sku_name'] = $bgoods->sku->sku_name;
            $re['goods_name'] = $bgoods->goods->goods_name;
            $re['sku_id'] = $bgoods->sku_id;
            $re['goods_id'] = $bgoods->goods_id;
            $data[] = $re;
        }
        return $data;
    }

    public static function emptyUserCart($user_id)
    {
        UserCartModel::where('user_id', $user_id)->delete();
        return new static(new Collection());
    }

    public static function emptySkuList($skuIds, $user_id)
    {
        return UserCartModel::whereIn('sku_id', $skuIds)->where('user_id', $user_id)->delete();
    }

    public static function emptyBunleList($bundleIds, $user_id)
    {
        return UserCartModel::where('bundle_id', $bundleIds)->where('user_id', $user_id)->delete();
    }

}